<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>Go Nginx反向代理 | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>Go Nginx反向代理</h1>
<p>当我们启动一个go 程序的时候，通常监听的是本地端口如 IP:port 。但是实际的线上环境，解析路径是：通过 DNS 解析 ——&gt; nginx vhost ——&gt; IP:port 这个。单纯的使用 go 是不能处理 域名解析工作的。
所以通常是NGINX 和 Go 搭配着使用。</p>
<p>nginx 可以帮我做很多工作，例如访问日志，cc 攻击，静态服务等，nginx 已经做的很成熟了。
Go 只要专注于业务逻辑和功能就好。
NGINX作为反向代理的配置如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-nginx" data-lang="nginx"><span style="display:flex;"><span><span style="color:#75715e">#列出所有服务器地址，nginx 自动均衡分发请求到各个服务器。 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">upstream</span> <span style="color:#e6db74">frontends</span> {   
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ip_hash</span>; 
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">server</span> 192.168.199.1:<span style="color:#ae81ff">8088</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">server</span> 192.168.199.2:<span style="color:#ae81ff">8089</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">server</span>
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">server_name</span> <span style="color:#e6db74">nav.v5u.win</span> ;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">#静态资源交由nginx管理
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>   	 <span style="color:#f92672">location</span> <span style="color:#e6db74">/static</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">root</span>        <span style="color:#e6db74">/var/www/mydomain/web</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">expires</span>     <span style="color:#e6db74">1d</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">add_header</span>  <span style="color:#e6db74">Cache-Control</span> <span style="color:#e6db74">public</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">access_log</span>  <span style="color:#66d9ef">off</span>;
</span></span><span style="display:flex;"><span>    	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>		<span style="color:#75715e"># 反向代理设置
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		<span style="color:#f92672">location</span> ~ <span style="color:#e6db74">/</span>
</span></span><span style="display:flex;"><span>			{
</span></span><span style="display:flex;"><span>				<span style="color:#f92672">proxy_set_header</span> <span style="color:#e6db74">X-Forwarded-For</span> $proxy_add_x_forwarded_for;
</span></span><span style="display:flex;"><span>				<span style="color:#f92672">proxy_set_header</span> <span style="color:#e6db74">Host</span> $http_host;
</span></span><span style="display:flex;"><span>				<span style="color:#f92672">proxy_redirect</span> <span style="color:#66d9ef">off</span>;
</span></span><span style="display:flex;"><span>				<span style="color:#f92672">proxy_pass</span> <span style="color:#e6db74">http://localhost:7070</span>;
</span></span><span style="display:flex;"><span>			}
</span></span><span style="display:flex;"><span>	}
</span></span></code></pre></div>
    </article>
  </div>

  
  
  

  
</body>

</html>